<?php

namespace doujunyu\utility\ali;

use think\Exception;


/**
 *
 * 快递物流查询接口 -API接口 （四川涪擎大数据技术有限公司）
 * https://market.aliyun.com/apimarket/detail/cmapi022273#sku=yuncode16273000014
 * Class AliExpressageCheck
 * @package App\Service
 */
class AliExpressage
{

    /**
     * 快递物流查询
     * @param string $appcode 开通服务后 买家中心-查看AppCode
     * @param string $no 快递单号
     * @param string $type 快递公司代码填了查询更快【代码见附表】
     */
    public static function kdwlcx(string $appcode,string $no,string $type = '')
    {
        $host = "https://kdwlcxf.market.alicloudapi.com";
        $path = "/kdwlcx";
        $method = "GET";
        // $appcode = "545516b3cd6844cba9f6c210e4b1d46a";//开通服务后 买家中心-查看AppCode
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "no={$no}&type={$type}";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $out_put = curl_exec($curl);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        list($header, $body) = explode("\r\n\r\n", $out_put, 2);
        if ($httpCode == 200) {
            $arr = json_decode($body,true);
            if(!isset($arr['status'])){
                return ['格式错误'.$body,[]];
            }
            if( $arr['status'] !== "0"){
                return ['物流信息错误：'.$arr['msg'],[]];
            }
            return ['',$arr];

        } else {
            if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
                return ['参数错误',''];
            } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
                return ['AppCode错误',''];
            } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
                return ['请求的 Method、Path 或者环境错误',''];
            } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
                return ['服务未被授权（或URL和Path不正确）',''];
            } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
                return ['套餐包次数用完',''];
            } elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
                return ['套餐包次数用完，请续购套餐',''];
            } elseif ($httpCode == 500) {
                return ['API网关错误',''];
            } elseif ($httpCode == 0) {
                return ['URL错误',''];
            } else {
                $headers = explode("\r\n", $header);
                $headList = array();
                foreach ($headers as $head) {
                    $value = explode(':', $head);
                    $headList[$value[0]] = $value[1];
                }
                return ['参数名错误 或 其他错误',['message'=>'HttpCode:'.$httpCode.'message:'.$headList['x-ca-error-message']]];
            }
        }

    }

}